<?php
require_once('inc/init.php');
require_once('inc/pager.php');
require_once getCacheFilePath('forums.php');

if(!isLogin()){
	_header_("location:login.php?ref=".urlencode($_SERVER["REQUEST_URI"]) );
}

$headtitle="个人中心";
$member=$db->row_select_one("users","id={$lg['userid']}");

$guidemenu='';
$guidemenu.='<p><a href="member.php?action=pic">我的头像</a></p>';
$guidemenu.='<p><a href="member.php?action=details">个人资料</a></p>';
$guidemenu.='<p><a href="member.php?action=password">密码设置</a></p>';
//$guidemenu.='<p><a href="member.php?action=storehouse">我的仓库</a></p>';
//$guidemenu.='<p><a href="member.php?action=friend">我的好友</a></p>';
$guidemenu.='<p><a href="member.php?action=msg">站内短信</a></p>';
//$guidemenu.='<p><a href="member.php?action=credits">我的积分</a></p>';
$guidemenu.='<p><a href="member.php?action=group">我的用户组/积分</a></p>';
$guidemenu.='<p><a href="member.php?action=mytopic">我发表的帖子</a></p>';
$guidemenu.='<p><a href="member.php?action=replytopic">我参与的帖子</a></p>';
$actiontitle='<span>个人中心</span>  » ';

$includepath='';
$action=strFilter($_GET['action']);
switch($action){
	case "":
	case "details":
		$actiontitle.='<span>个人资料</span>';
		$includepath='member_details.htm';
		break;
	case "pic":
		$actiontitle.='<span>我的头像</span>';
		$includepath='member_pic.htm';
		$userpic_file = INC_P."/../uploadfile/userpic/origin/{$lg['userid']}.jpg";
		if(file_exists($userpic_file)){
			unlink($userpic_file);
		}
		break;	
	case "password":
		$actiontitle.='<span>密码设置</span>';
		$includepath='member_password.htm';
		break;	
		
	case "msg":
		$actiontitle.='<span>站内短信</span>';
		$includepath='member_msg.htm';
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init($cache_settings['msgpernum'],$curPage,"member.php?action=msg&page={page}");
		$rows=$pager->queryRows($db,"msgs","touserid={$lg['userid']}","id,fromuserid,fromuser,title,posttime,hasread","id desc");
		foreach($rows as $key=>$row){
			$row['css']=$row['hasread']==1?'hasread':'unread';
			$row['title']=htmlFilter($row['title']);
			$row['posttime']=getDateStr($row['posttime']);
			if($row['fromuserid']==0){
				$row['fromuser']="<span class='msg_system'>系统消息</span>";
			}else{
				$row['fromuser']="<a href='".genUrl("user.php?uid={$row['fromuserid']}")."' target='_blank'>".htmlFilter($row['fromuser'])."</a>";
			}
			$rows[$key]=$row;
		}
		break;
	
	case "group":
		$actiontitle.='<span>我的用户组/积分</span>';
		$includepath='member_group.htm';
		$userrow=$db->row_select_one("users","id={$lg['userid']}","id,groupid,postsnum,credits1,credits2,credits3,credits4,credits5,credits6,credits7,credits8");
		$allmark=getAllCredits($cache_settings['creditsexpression'], $userrow);
		$creditsexp = $cache_settings['creditsexpression'];
		$creditsexp=str_replace(	array('postsnum', 'credits1', 'credits2', 'credits3', 'credits4', 'credits5', 'credits6', 'credits7', 'credits8' ), 
								array('帖数', $cache_credits['credits1']['creditsname'], $cache_credits['credits2']['creditsname'], $cache_credits['credits3']['creditsname'], $cache_credits['credits4']['creditsname'], $cache_credits['credits5']['creditsname'], $cache_credits['credits6']['creditsname'], $cache_credits['credits7']['creditsname'], $cache_credits['credits8']['creditsname']), 
								$creditsexp
							);
		$grouprow=$db->row_select_one("groups","groupid={$userrow['groupid']}","groupid,groupname,starnum");
		$grouprow['grouptype'] = $grouprow['groupid']>100 ? "系统组" : "普通组";
		$starimg=getStar($grouprow['starnum']);

	break;
	
	case "mytopic":
		$actiontitle.='<span>我发表的帖子</span>';
		$includepath='member_post.htm';
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init($cache_settings['topicspernum'],$curPage,"member.php?action={$action}&page={page}");
		$rows = $pager->queryRows($db,"topics","userid={$lg['userid']} and isrecycle=0 and verifystate=0", "*,topics.id as tid","id desc");
		foreach($rows as $key=>$row){
			$row['posttime']=getDateStr($row['posttime']);
			$forum=$cache_forums[$row['fid']];
			$row['title']=htmlFilter(cutStr($row['title'],50));
			$row['username']=htmlFilter($row['username']);
			$row['icon']=getTopicIcon($row);
			$row['forumurl']=genUrl("forum.php?fid={$row['fid']}");
			$row['viewurl']=genUrl("view.php?tid={$row['tid']}");
			$row['userurl']=genUrl("user.php?uid={$row['userid']}");
			$rows[$key]=$row;
		}
		$member_title="我发布的帖子";
	break;

	case "replytopic":
		$actiontitle.='<span>我参与的帖子</span>';
		$includepath='member_post.htm';
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init($cache_settings['topicspernum'],$curPage,"member.php?action={$action}&page={page}");
		$rows = $pager->queryRows($db,"topics","id in (select tid from `{$db->pre}posts` where userid={$lg['userid']} and istopic=0) and isrecycle=0 and verifystate=0 ", "*,topics.id as tid","id desc");
		foreach($rows as $key=>$row){
			$row['posttime']=getDateStr($row['posttime']);
			$forum=$cache_forums[$row['fid']];
			$row['title']=htmlFilter(cutStr($row['title'],50));
			$row['username']=htmlFilter($row['username']);
			$row['icon']=getTopicIcon($row);
			$row['forumurl']=genUrl("forum.php?fid={$row['fid']}");
			$row['viewurl']=genUrl("view.php?tid={$row['tid']}");
			$row['userurl']=genUrl("user.php?uid={$row['userid']}");
			$rows[$key]=$row;
		}
		$member_title="我参与的帖子";
	break;

	default:
		$includepath='member_details.htm';
	break;

}
require_once('header.php');
require_once getTemplatePath('member.htm');
footer();
?>